#ifndef __HEAD_H__
#define __HEAD_H__

#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <syslog.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pthread.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/sendfile.h>

#include "msgHandler.h"

#define FILE_PIECE_SIZE 1000

#define SIZE(a) (sizeof(a)/sizeof(a[0]))

#define ARGS_CHECK(argc, n) {               \
    if (argc != n) {                        \
        fprintf(stderr, "Error: expected %d arguments\n", n);   \
        exit(1);                            \
    }                                       \
}

#define ERROR_CHECK(retval, val, msg) {    \
    if (retval == val) {                   \
        perror(msg);                       \
        exit(1);                           \
    }                                      \
}
#define THREAD_ERROR_CHECK(ret,msg) {if(ret!=0){\
        fprintf(stderr,"%s:%s\n",msg,strerror(ret));}}

typedef enum {
    SENDF,
    RECVF
} trans_type;

typedef struct workFile_s{
    char filename[100];
    char filepath[100];
    ssize_t filesize;
    off_t offset;
    char md5[100];
    int s_flag; //1. 文件秒传完成结束传输 2.文件不存在
    char dbFilepath[100];
} workFile_t;

typedef struct user_s {
    char name[32];
    char passwd[256];
} user_t;
typedef struct cmd_s {
    char cmd[256];
    char args[1024];
} cmd_t;
//typedef struct cmd_s{
//    char operate[1024];
//    char object[1024];
//}cmd_t;

int tcpInit(char *ip,char *port);
int cmd_separate(const char* cmd,char* tokens[],int* tokenNum);

int cmdSplit(char *cmd,char **operate,char **object);
int recvn(int sockfd, void *buf, int n);
int sendOperate(char *op,int sockfd);
int isExist(char *fileName,int *fileExist);
int recvFile(int sockfd,char *fileName);
int sendFile(int sockfd,char *fileName);
 int is_cmd(char *operate);

int file_recvn(int sockfd, void *buf, int n);

int cTransFile(int sockfd, trans_type transt, char * cwp);

int workFile_init(int netfd, workFile_t* wf, char *cwp);

int tSendFile(int netfd, workFile_t *wf);

int tRecvFile(int netfd, workFile_t *wf);

char *getFileName(char *filepath);

bool file_exist(char *filepath);

int sendPrintInfo(int netfd, char *info);

int sendFileInfo(int netfd, workFile_t *wf);

int recvFileInfo(int netfd, workFile_t *wf);

int recvFileDta(int netfd, train_t *train);

int sendFileData(int netfd, train_t *train);

int sendCMDEnd(int netfd);

int cWorkFile_init(workFile_t *wf, char *cwp);
int recv_msg(const int netfd, train_t *pTrain);
int send_msg(const int netfd, train_t *pTrain);

#endif

